/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.modules.app.utils;


import io.renren.common.exception.RRException;
import io.renren.common.utils.CheckParam;
import io.renren.modules.app.entity.CustomerEntity;
import org.springframework.http.HttpStatus;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 工具类
 *
 * @author MR.xu 260139486@qq.com
 */
public class AuthUtils {
	public static CustomerEntity getUser() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();

		//获取用户信息
        CustomerEntity user = (CustomerEntity)request.getAttribute("user");
        if(CheckParam.isNull(user)){
            throw new RRException("未获取到身份信息，请重新登录", HttpStatus.UNAUTHORIZED.value());
        }
		return user;
	}
//
	public static Integer getUserId() {
//		UserEntity user = getUser();
//		return null == user ? -1L : user.getUserId();
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        Integer userId = (int)request.getAttribute("userId");
        if(CheckParam.isNull(userId)){
            throw new RRException("未获取到身份信息，请重新登录", HttpStatus.UNAUTHORIZED.value());
        }
        return userId;
	}
}
